Wow this is an old topic!
But... I was working on a project and thought that anyone who was looking for a NeoPixel candle example that isn't blocking, well this would be the place. It flickers and flutters like a real candle. The only thing is that the flame isn't illuminated like these nice Luminaras, but I'm too cheap to buy them!
Keep in mind that my example here uses GRB LEDs, but you merely need to reorder the variables in order to get this working for RGB. Plus the Time Library is not Arduino, so you will have to play with that too for a good randomSeed().
It looks pretty realistic with even a single neoPixel in a sheet of A4 paper rolled into a cylinder. I'll post a video when I can.
Have fun with it:
#include "neopixel.h"
enum CandleStates{
BURN_CANDLE,
FLICKER_CANDLE,
FLUTTER_CANDLE,
MODES_MAX_CANDLE
};
enum PixelSelect{
EVERY_PIXEL,
SINGLE_PIXEL,
};
class Candle : public Adafruit_NeoPixel
{
public:
Candle(uint16_t count, uint8_t pin, uint8_t type);
Candle(uint16_t count, uint8_t pin, uint8_t type, PixelSelect pixel, uint32_t pixNum = 0);
~Candle(){};
void update();
private:
bool fire(uint8_t greenDropValue, uint32_t cycleTime);
PixelSelect _pixelMode = EVERY_PIXEL;
uint32_t _pixNum = 0;
CandleStates _mode;
uint32_t _lastModeChange;
uint32_t _modeDuration;
uint8_t _redPx = 255;
uint8_t _bluePx = 10; //10 for 5v, 15 for 3.3v
uint8_t _grnHigh = 100; //110-120 for 5v, 135 for 3.3v
uint8_t _grnPx = 100;
uint32_t _lastBurnUpdate = 0;
int _direction = 1;
};
Candle::Candle(uint16_t count, uint8_t pin, uint8_t type) : Adafruit_NeoPixel(count, pin, type)
{
randomSeed(Time.now() + micros());
_mode = BURN_CANDLE;
}
Candle::Candle(uint16_t count, uint8_t pin, uint8_t type, PixelSelect pixel, uint32_t pixNum) : Adafruit_NeoPixel(count, pin, type)
{
_pixelMode = pixel;
_pixNum = pixNum;
}
void Candle::update()
{
if(millis() - _lastModeChange > _modeDuration)
{
_mode = static_cast<CandleStates>(random(MODES_MAX_CANDLE));
_modeDuration = random(1000, 8000);
_lastModeChange = millis();
//Serial.printlnf("New state: %d\tTime: %d", static_cast<int>(_mode), _modeDuration);
}
switch(_mode)
{
case BURN_CANDLE:
this->fire(10, 120);
break;
case FLICKER_CANDLE:
this->fire(15, 120);
break;
case FLUTTER_CANDLE:
this->fire(30, 120);
break;
};
}
bool Candle::fire(uint8_t greenDropValue, uint32_t cycleTime)
{
int currentMillis = millis();
if(currentMillis - _lastBurnUpdate > (cycleTime / greenDropValue / 2))
{
_grnPx = constrain(_grnPx += _direction, _grnHigh - greenDropValue, _grnHigh);
if(_grnPx == _grnHigh - greenDropValue or _grnPx == _grnHigh)
{
_direction *= -1;
}
switch (_pixelMode)
{
case EVERY_PIXEL:
for(int i = 0; i < this->numPixels(); i++)
{
this->setPixelColor(i, _grnPx, _redPx, _bluePx);
}
break;
case SINGLE_PIXEL:
this->setPixelColor(_pixNum, _grnPx, _redPx, _bluePx);
break;
}
this->show();
_lastBurnUpdate = currentMillis;
}
}
#define PIXEL_COUNT 2
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812B // I'M USING GRB WS2821B's here
Candle candle = Candle(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE, SINGLE_PIXEL);
void setup(void)
{
Serial.begin(115200);
pinMode(13, OUTPUT);
candle.begin();
candle.show();
Serial.println("Started program");
}
void loop(void)
{
candle.update();
static uint32_t lastFlashMillis = 0;
if(millis() - lastFlashMillis > 250)
{
digitalWrite(13, !digitalRead(13));
lastFlashMillis = millis();
}
}